package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MyThread implements Runnable {
    private static final String TAG = "MyThread";
    Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void run() {
        Log.i(TAG, "run: ()...............");
        Bundle bundle = new Bundle();
        List<String> retlist = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect("https://www.huilvzaixian.com/").get();
            Elements uls = doc.getElementsByTag("ul");
            Element ul = uls.get(3);
            Elements lis = ul.getElementsByTag("li");
            for (Element li : lis) {
                Elements as = li.getElementsByTag("a");
                if (as.size() > 0) {
                    for (Element a : as) {
                        String rname = a.text();
                        Log.i(TAG, "run: " + rname);
                        retlist.add(rname);
                        if (rname.equals("欧元")) {
                            Log.i(TAG, "run: 欧元汇率=");
//bundle.putFloat("web_euro", 100 / Float.parseFloat(rateStr));
                        } else if (rname.equals("韩国元")) {
                            Log.i(TAG, "run: 韩元汇率=");
//bundle.putFloat("web_won", 100 / Float.parseFloat(rateStr));
                        } else if (rname.equals("美元")) {
                            Log.i(TAG, "run: 美元汇率=");
//bundle.putFloat("dollar_won", 100 / Float.parseFloat(rateStr));
                        }
                    }
                }
            }
        } catch (IOException e) {

        }

        // 发送数据回主线程
        if (handler != null) {
            Message msg = handler.obtainMessage(6);
            msg.obj = bundle;
            handler.sendMessage(msg);
            Log.i(TAG, "run: msg已发送");

            // 返回 list
            Message msg2 = handler.obtainMessage(7, retlist);
            handler.sendMessage(msg2);
        } else {
            Log.e(TAG, "run: handler为null，无法发送消息");
        }
    }
}
